/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package app.delish.details

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.darkColors
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BookmarkBorder
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.IosShare
import androidx.compose.material.icons.filled.Stars
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.delish.details.R
import com.elbehiry.model.RecipesItem

/**
 * 食谱操作选项栏：展示点赞、评分、收藏和分享等功能按钮
 *
 * @param recipe 食谱数据对象
 * @param onSave 收藏按钮点击回调（传递食谱对象）
 */
@Composable
fun RecipeOptions(
    recipe: RecipesItem,             // 当前食谱数据
    onSave: (RecipesItem) -> Unit     // 收藏操作回调
) {
    // 水平布局容器
    Row(
        modifier = Modifier.padding(start = 8.dp, end = 8.dp)  // 左右内边距
    ) {
        // 1. 点赞按钮
        TextButton(
            modifier = Modifier
                .weight(1f)  // 平均分配宽度
                .align(Alignment.CenterVertically), // 垂直居中
            onClick = {}  // 当前无操作
        ) {
            // 爱心图标 + 点赞数
            Icon(
                modifier = Modifier.padding(end = 4.dp),  // 图标右侧间距
                imageVector = Icons.Filled.Favorite,       // 爱心图标
                contentDescription = null,                 // 无需辅助描述
                tint = MaterialTheme.colors.primary        // 使用主题主色调（表示激活）
            )
            Text(
                text = "${recipe.aggregateLikes ?: 0}",   // 显示点赞数（为空时默认0）
                color = Color.White,                       // 白色文本
                style = MaterialTheme.typography.subtitle2, // 小号副标题样式
                fontSize = 12.sp                           // 固定12sp字号
            )
        }

        // 2. 评分按钮
        TextButton(
            modifier = Modifier
                .weight(1f)
                .align(Alignment.CenterVertically),
            onClick = {}  // 当前无操作
        ) {
            // 星星图标 + 评分
            Icon(
                modifier = Modifier.padding(end = 4.dp),
                imageVector = Icons.Filled.Stars,          // 评分图标
                contentDescription = null,
                tint = Color.White                         // 白色图标
            )
            Text(
                text = "${(recipe.spoonacularScore?:0).toInt()}", // 显示评分（转为整数）
                color = Color.White,
                style = MaterialTheme.typography.subtitle2,
                fontSize = 12.sp
            )
        }

        // 3. 收藏按钮（唯一有实际功能的按钮）
        TextButton(
            modifier = Modifier
                .weight(1f)
                .align(Alignment.CenterVertically),
            onClick = {
                onSave(recipe)  // 点击时触发收藏回调
            }
        ) {
            // 书签图标 + "收藏"文本
            Icon(
                modifier = Modifier.padding(end = 4.dp),
                imageVector = Icons.Filled.BookmarkBorder, // 边框书签图标（表示未收藏）
                contentDescription = null,
                tint = Color.White
            )
            Text(
                text = stringResource(id = R.string.save), // 多语言"收藏"文本
                color = Color.White,
                style = MaterialTheme.typography.subtitle2,
                fontSize = 12.sp
            )
        }

        // 4. 分享按钮
        TextButton(
            modifier = Modifier
                .weight(1f)
                .align(Alignment.CenterVertically),
            onClick = {}  // 当前无操作
        ) {
            // 分享图标 + "分享"文本
            Icon(
                modifier = Modifier.padding(end = 4.dp),
                imageVector = Icons.Filled.IosShare,        // iOS风格分享图标（Android适用）
                contentDescription = null,
                tint = Color.White
            )
            Text(
                text = stringResource(id = R.string.share), // 多语言"分享"文本
                color = Color.White,
                style = MaterialTheme.typography.subtitle2,
                fontSize = 12.sp
            )
        }
    }

    // 底部间距
    Spacer(modifier = Modifier.height(10.dp))
}

/**
 * 预览函数：展示食谱操作选项栏的不同状态
 */
@Preview(showBackground = true, backgroundColor = 0xFF333333)  // 深灰色背景模拟应用主题
@Composable
private fun RecipeOptionsPreview() {
    // 1. 创建高点赞数的测试食谱
    val popularRecipe = RecipesItem(
        id = 1,
        title = "Classic Cheeseburger",
        aggregateLikes = 1250,
        spoonacularScore = 92.3
    )

    // 2. 创建中等分数的测试食谱
    val averageRecipe = RecipesItem(
        id = 2,
        title = "Vegetable Stir-Fry",
        aggregateLikes = 320,
        spoonacularScore = 76.8
    )

    // 3. 创建低分数的测试食谱
    val basicRecipe = RecipesItem(
        id = 3,
        title = "Simple Omelette",
        aggregateLikes = 45,
        spoonacularScore = 62.5
    )
    RecipeOptions(
        recipe = popularRecipe,
        onSave = { println("保存食谱: ${it.title}") }
    )

}